package br.ucb.sigplan.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import br.ucb.sigplan.beans.Conteudo;
import br.ucb.sigplan.connection.FactoryCon;
import br.ucb.sigplan.dao.ConteudoDAO;
import br.ucb.sigplan.exception.DAOException;

public class ConteudoDAOImpl implements ConteudoDAO {
	private final String SQL_INSERT = "insert into conteudo (descricao, tipo) values (?,?)";
	private final String SQL_FINDALL = "select * from conteudo";
	private final String SQL_FINDBYTIPO = "select * from conteudo where tipo = ?";
	private final String SQL_SEARCHBYID = "select * from conteudo where idconteudo = ?";
	private final String SQL_DELETE = "delete from conteudo where idconteudo = ?";
	private final String SQL_UPDATE = "update conteudo set descricao = ? where idconteudo = ?";
	
	public List<Conteudo> findAll() throws DAOException {
		List<Conteudo> conteudos = new ArrayList<Conteudo>();
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		ResultSet set = null;
		Conteudo conteudo;
		try {
			instrucao = con.prepareStatement(SQL_FINDALL);
			set = instrucao.executeQuery();
			while (set.next()) {
				conteudo = new Conteudo();
				putConteudo(conteudo, set);
				conteudos.add(conteudo);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("erroPesquisa");
		} finally {
			FactoryCon.closeConnection(con, instrucao, set);
		}
		return conteudos;
	}

	public void insert(Conteudo conteudo) throws DAOException {
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		System.out.println("Chegou");
		try {
			instrucao = con.prepareStatement(SQL_INSERT);
			instrucao.setString(1, conteudo.getDescricao());
			instrucao.setString(2, conteudo.getTipo().toString());
			instrucao.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("erroInsercao");
		} finally {
			FactoryCon.closeConnection(con, instrucao);
		}
	}
	
	public void putConteudo(Conteudo conteudo, ResultSet set) throws SQLException {
		conteudo.setIdConteudo(set.getInt("idconteudo"));
		conteudo.setDescricao(set.getString("descricao"));
		conteudo.setTipo(set.getString("tipo").charAt(0));
	}

	public Conteudo findById(Integer idconteudo) throws DAOException {
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		ResultSet set = null;
		Conteudo conteudo = null;
		try {
			instrucao = con.prepareStatement(SQL_SEARCHBYID);
			instrucao.setInt(1, idconteudo);
			set = instrucao.executeQuery();
			if (set.next()) {
				conteudo = new Conteudo();
				putConteudo(conteudo, set);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("erroPesquisa");
		} finally {
			FactoryCon.closeConnection(con, instrucao, set);
		}
		return conteudo;
	}

	public void delete(Integer idconteudo) throws DAOException {
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		try {
			instrucao = con.prepareStatement(SQL_DELETE);
			instrucao.setInt(1, idconteudo);
			instrucao.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("conteudoDelecao");
		} finally {
			FactoryCon.closeConnection(con, instrucao);
		}
	}

	@Override
	public void update(Conteudo conteudo) throws DAOException {
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		try {
			instrucao = con.prepareStatement(SQL_UPDATE);
			instrucao.setString(1, conteudo.getDescricao());
			instrucao.setInt(2, conteudo.getIdConteudo());
			instrucao.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("erroAtualizacao");
		} finally {
			FactoryCon.closeConnection(con, instrucao);
		}
		
	}

	@Override
	public List<Conteudo> findByTipo(Character tipo) throws DAOException {
		List<Conteudo> conteudos = new ArrayList<Conteudo>();
		Connection con = FactoryCon.getInstance().createConnection();
		PreparedStatement instrucao = null;
		ResultSet set = null;
		Conteudo conteudo;
		try {
			instrucao = con.prepareStatement(SQL_FINDBYTIPO);
			instrucao.setString(1, tipo.toString());
			set = instrucao.executeQuery();
			while (set.next()) {
				conteudo = new Conteudo();
				putConteudo(conteudo, set);
				conteudos.add(conteudo);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException("erroPesquisa");
		} finally {
			FactoryCon.closeConnection(con, instrucao, set);
		}
		return conteudos;
	}

}
